package logging;


import java.io.IOException;
import java.util.ArrayList;
import java.util.Properties;
import java.io.FileInputStream;
import java.io.InputStream;













import Filter.Filter;
import Filter.LevelFilter;
import Filter.TermFilter;
import Filter.DateFilter;
import Filter.CustomFilter;
import Filter.RegularExpressionFilter;

public class PropertiesConfig extends Config {

	
	public PropertiesConfig(String path){
		this.path = path;
		this.enableConsole=false;
		this.separator="-";
		this.hasConsoleFilter=false;
		this.filterConsole=null;
		this.pathValid=(fileExist() && path.contains(".propertie"));
	}
	
	
	private void loadFilterCustom(Properties prop, String file, int count) throws Exception {
		
		String nameClase=prop.getProperty("fileNameClase" + count);
		String listParametersValues=prop.getProperty("listParametersFilter" + count, null);
		ArrayList<String> listParameters = new ArrayList<String>();
		if (listParametersValues != null)
			listParameters=this.slipParameters(listParametersValues);
		
		Object[] arg= null;

		if (listParameters.size() > 0){
			arg = new Object[listParameters.size()];
			for (int i=0; i<listParameters.size(); i++){
				arg [i] =  listParameters.get(i);
			}
		}else{
			arg = new Object[0];
		}
					
	
		Filter filter = new Filter (new CustomFilter(nameClase, arg));		
		this.files.put(file, filter);					
	}
		

	
	private void loadFilterFiles(Properties prop){		
		int countFile;
		String valueFilter, file, typeFilter, parameters, nameClase;
		Filter filter;
	
		
		countFile=Integer.parseInt(prop.getProperty("countFiles"));		
		for (int count=1; count<=countFile; count++){
			file=prop.getProperty("file"+ count);
			if (file.equalsIgnoreCase("DestinoCustom")){
				nameClase=prop.getProperty("nameClase"+ count);
				parameters=prop.getProperty("listParameters"+ count, null);
				ArrayList<String> listParameters = new ArrayList<String>();
				if (parameters != null)
					listParameters=this.slipParameters(parameters);
				this.destinationCustom.put(nameClase,listParameters);				
				file=nameClase;
			}
			
				typeFilter=prop.getProperty("fileTypeFilter"+ count);

				if (typeFilter.equals("CUSTOM")) 	{	
					try {
						loadFilterCustom(prop, file, count);
					} catch (Exception e) {

					e.printStackTrace();
					}
				}
				else {
					valueFilter=prop.getProperty("fileValueFilter"+ count);
					if (typeFilter.equals("TERM")){
						filter = new Filter (new TermFilter(valueFilter));
					this.files.put(file, filter);					
					}
					else
						if (typeFilter.equals("LEVEL")) {		
							filter = new Filter (new LevelFilter(valueFilter));	
							this.files.put(file, filter);					
						}
						else
							if (typeFilter.equals("DATE")) {	
								filter = new Filter (new DateFilter(valueFilter));	
								this.files.put(file, filter);					
							}
							else
								if (typeFilter.equals("REGULAREXPRESSION")) {
									filter = new Filter (new RegularExpressionFilter(valueFilter));	
									this.files.put(file, filter);					
								}
								else						
									this.files.put(file, null);							     
				}
			
							
		}
	}
	
	
	private void loadFilterConsole (Properties prop) throws Exception {		
		String valueFilter, typeFilter, consoleNameClase;
		
		typeFilter=prop.getProperty("consoleTypeFilter");
		this.hasConsoleFilter=true;
		this.filterConsole = null;	
		if (typeFilter.equals("CUSTOM")){
			consoleNameClase=prop.getProperty("consoleNameClase");							
			String listParametersValues=prop.getProperty("consoleListParameters", null);
			ArrayList<String> listParameters = new ArrayList<String>();
			if (listParametersValues != null)
				listParameters=this.slipParameters(listParametersValues);
			
			Object[] arg= null;

			if (listParameters.size() > 0){
				arg = new Object[listParameters.size()];
				for (int i=0; i<listParameters.size(); i++){
					arg [i] =  listParameters.get(i);
				}
			}else{
				arg = new Object[0];
			}
		
												
			this.filterConsole = new Filter (new CustomFilter(consoleNameClase, arg));		
														
		}
				
		else{
			 valueFilter=prop.getProperty("consoleValueFilter");
			 if (typeFilter.equals("TERM")){
				this.filterConsole = new Filter (new TermFilter(valueFilter));				
			 }
			 else
				 if (typeFilter.equals("LEVEL")) {										
					 this.filterConsole = new Filter (new LevelFilter(valueFilter));					
				 }
				 else
					 if (typeFilter.equals("DATE")) {										
						 this.filterConsole = new Filter (new DateFilter(valueFilter));
					 }
					 else
						 if (typeFilter.equals("REGULAREXPRESSION")) {										
							 this.filterConsole = new Filter (new RegularExpressionFilter(valueFilter));
						 }
						 else
							 this.hasConsoleFilter=false;
		}
	}
	
	public void readConfig(){		 
		Properties prop = new Properties();
		InputStream input = null;

		try {		
			input = new FileInputStream(this.path);
		 
			// load a properties file
			prop.load(input);
		 
			// get the property value and print it out
			this.formatConsole=prop.getProperty("formatConsole");
			this.formatFiles=prop.getProperty("formatFiles");
			this.separator=prop.getProperty("separator");
			this.level=prop.getProperty("levelDefault");
			
			if (prop.getProperty("console").equals("yes"))
				this.enableConsole=true;
		
//			this.typeFormatConsole=prop.getProperty("typeFormatConsole");
			this.typeFormat=prop.getProperty("typeFormat");
						
			try {
				this.loadFilterConsole(prop);
			} catch (Exception e) {

			e.printStackTrace();
			}
			
			
			this.loadFilterFiles(prop);
            
            
													 
			} catch (IOException ex) {
				
				
		    this.pathValid=false;				
				
			} finally {
				if (input != null) {
					try {
						input.close();
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
			}		 
	 }
	
	

	 
}